package org.blog.service;

import java.util.List;

import org.blog.config.KeyChange;
import org.blog.dao.BanIPDao;
import org.blog.helper.Result;
import org.blog.model.ActionType;
import org.blog.model.ModelBanIP;
import org.blog.model.ModelPassport;
import org.blog.service.util.ServiceConstants;
import org.blog.service.util.ValidateHelper;
import org.springframework.beans.factory.annotation.Autowired;

public class BanIPServiceImpl implements BanIPService{
	
	@Autowired
	private BanIPDao banIPDao;
	

	@Override
	public Result<?> addBanIP(ModelBanIP t, ModelPassport passport) {
		if(passport!=null&&passport.getRole()>ServiceConstants.ADMIN){
			if(ValidateHelper.validate(t, ActionType.ADD)){
				
				t.setCreateTime(System.currentTimeMillis()/1000);
				t.setUsername(passport.getUsername());
				
				banIPDao.addBanIP(t);
				KeyChange.remove(KeyChange.banIP);
				return new Result<Object>(200);
			}else{
				return new Result<Object>(400);
			}
		}else{
			return new Result<Object>(401);
		}
	}

	@Override
	public Result<?> modifyBanIP(ModelBanIP t, ModelPassport passport) {
		if(passport!=null&&passport.getRole()>ServiceConstants.ADMIN){
			if(ValidateHelper.validate(t, ActionType.MODIFY)){
				
				banIPDao.addBanIP(t);
				KeyChange.remove(KeyChange.banIP);
				return new Result<Object>(200);
			}else{
				return new Result<Object>(400);
			}
		}else{
			return new Result<Object>(401);
		}
	}

	@Override
	public Result<ModelBanIP> getBanIP(ModelBanIP t, ModelPassport passport) {
		if(passport!=null&&passport.getRole()>ServiceConstants.ADMIN){
			if(ValidateHelper.validate(t, ActionType.GET)){
				
				ModelBanIP banIP=banIPDao.getBanIP(t.getId());
				return new Result<ModelBanIP>(banIP,200);
			}else{
				return new Result<ModelBanIP>(400);
			}
		}else{
			return new Result<ModelBanIP>(401);
		}
	}

	@Override
	public Result<List<ModelBanIP>> getBanIPList(ModelPassport passport) {
		if(passport!=null&&passport.getRole()>ServiceConstants.ADMIN){
				
				List<ModelBanIP> list=banIPDao.getList();
				return new Result<List<ModelBanIP>>(list,200);
		}else{
			return new Result<List<ModelBanIP>>(401);
		}
	}
	public Result<List<ModelBanIP>> getBanIPSelectList(ModelBanIP t,ModelPassport passport){
		if(passport!=null&&passport.getRole()>ServiceConstants.ADMIN){
			if(ValidateHelper.validate(t, ActionType.SELECT)){
				
				List<ModelBanIP> list=banIPDao.getBanIPSelectList(t);
				return new Result<List<ModelBanIP>>(list,200);
			}else{
				return new Result<List<ModelBanIP>>(400);
			}
		}else{
			return new Result<List<ModelBanIP>>(401);
		}
	}
	
	
}
